--[[
Author:  Javek
ReleaseDate: 3-24-2007
--]]

function GnomeCruncher:GuiInit()
	local f = CreateFrame("Frame",nil,UIParent)
	f:SetHeight(20)
	f:SetWidth(20)
		
	local tex = f:CreateTexture(nil,"BACKGROUND")
	tex:SetTexture("Interface\\Icons\\Spell_ChargePositive")
	tex:SetAllPoints(f)
	f:SetPoint("TOPLEFT",UIParent,"BOTTOMLEFT",
		self.db.char.xpos,self.db.char.ypos)
	f:EnableMouse(true)
	f:SetMovable(true)
	
	f:SetScript("OnMouseDown",
	function()
		if arg1 == "RightButton" then
			f:StartMoving()
		else
			GnomeCruncher:expand()
		end
	end)
	
	f:SetScript("OnMouseUp",
	function()
		f:StopMovingOrSizing()
		GnomeCruncher:savePosition()
	end)
	
	f:SetScript("OnEnter",
	function()
		GameTooltip_SetDefaultAnchor(GameTooltip,UIParent)
		GameTooltip:AddLine("GnomeCruncher Instructions:")
		GameTooltip:AddLine("Left-Click: Expand/Minimize the calculator.")
		GameTooltip:AddLine("RightButton-Drag: Move the calculator anchor.")
		GameTooltip:AddLine("Right-Clicking the results removes them.")
		GameTooltip:AddLine("Type '/gnomecruncher help' in the chat window for more information.")
		GameTooltip:Show()
	end)
	f:SetScript("OnLeave",
	function()
		GameTooltip:Hide()
	end)
	
	
	
	if self.db.char.showing then
		f:Show()
	else
		f:Hide()
	end
	self.mainFrame = f

	
--	f = CreateFrame("EditBox",nil,self.mainFrame,"GnomeCruncherEntryBoxTemplate")
	self.entryFrame = getglobal("GnomeCruncherEntryBox")
	if self.db.char.horizDir then
		-- To Left
		self.entryFrame:SetPoint("TOPRIGHT",self.mainFrame,"TOPLEFT",0,3)
	else
		self.entryFrame:SetPoint("TOPLEFT",self.mainFrame,"TOPRIGHT",0,3)
	end
	self.entryFrame:SetParent(self.mainFrame)
	self.entryFrame:SetWidth(self.db.char.width)
	
	self.expanded = false
	self.frameStack = {}
	self.resultFrames = {}
end

function GnomeCruncher:expand()
	--expands or minimizes calculator
	if self.expanded then
		--minimize
		self.entryFrame:Hide()
		self.expanded = false
		for n,f in pairs(self.resultFrames) do
			f:Hide()
		end
	else
		--expand
		self.entryFrame:Show()
		self.expanded = true
		for n,f in pairs(self.resultFrames) do
			f:Show()
		end
		self.entryFrame:SetFocus()
	end
end

function GnomeCruncher:reset()
	self.mainFrame:SetPoint("TOPLEFT",UIParent,"BOTTOMLEFT",
		UIParent:GetWidth()/2,UIParent:GetHeight()/2)
	self:savePosition()
end

--Saves the position
function GnomeCruncher:savePosition()
	self.db.char.xpos = self.mainFrame:GetLeft()
	self.db.char.ypos = self.mainFrame:GetTop()
	self:LevelDebug(3,string.format("Pos Saved xpos: %d  ypos: %d",self.db.char.xpos,self.db.char.ypos))
end

function GnomeCruncher:updateFramePosition(n)
	if n == 0 then return end
	--Note: Frames are aranged from the outside most being 1 to n being the innermost
	local f = self.resultFrames[n]
	if f == nil then
		self:Debug("Warning: resultFrames["..n.."] was nil!")
		return
	end
	f:ClearAllPoints()
	local lowerFrame = nil
	if n == table.getn(self.resultFrames) then
		lowerFrame = self.mainFrame
	else
		lowerFrame = self.resultFrames[n+1]
	end
	
	assert(P1 == nil)
	assert(P2 == nil)
	assert(P3 == nil)
	
	local P1 = ""
	local P2 = ""
	local P3 = ""
	
	if self.db.char.vertDir then
		--Going Up
		P1 = "BOTTOM"
		P2 = "TOP"
	else
		--Going Down
		P1 = "TOP"
		P2 = "BOTTOM"
	end
	
	if self.db.char.horizDir then
		--On Left
		P1 = P1.."RIGHT"
		P2 = P2.."RIGHT"
		P3 = "LEFT"		
	else
		--On Right
		P1 = P1.."LEFT"
		P2 = P2.."LEFT"
		P3 = "RIGHT"
	end
	self:Debug( ("P1:%s  P2:%s  P3:%s"):format(P1,P2,P3) )
	f:SetPoint(P1,lowerFrame,P2)
	f:SetPoint(P3,self.entryFrame)
end

